//
//  MyBillListCell.swift
//  cxd4iphone
//
//  Created by hexy on 12/15/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

private struct kConstraints {
    
    static let wrapEdges: UIEdgeInsets = UIEdgeInsetsMake(0, 10, -10, -10)
    static let margin: CGFloat         = 10
    static let detailLabelMaxWrapWidth: CGFloat         = xx_screenWidth() - 2 * kConstraints.margin
    static let topViewHeight: CGFloat  = xx_height(40)
    
    static let col1Width: CGFloat      = xx_width(70)
    static let col2Width: CGFloat      = ((xx_screenWidth() - 2 * kConstraints.margin) - kConstraints.col1Width - kConstraints.col4Width)/2
    
    static let col3Width: CGFloat      = kConstraints.col2Width
    static let col4Width: CGFloat      = xx_width(64)
    
}

class MyBillListCell: BaseTableViewCell {

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        self.selectionStyle = .None
        
    }
    
    override func updateUI(viewModel: BaseCellViewModel) {
        
        guard let vm = (viewModel as? MyBillCellViewModel) else { return }
        
//        xx_print(vm.userBillModel.typeInfo)
        typeLabel.text = vm.userBillModel.typeInfo!
        balanceLabel.text = "\(vm.userBillModel.balance)"
        moneyLabel.text = "\(vm.userBillModel.money)"
        dayLabel.text = vm.userBillModel.time!
        detailLabel.text = vm.userBillModel.detail!
    }
    override func makeUI() {
        self.accessoryType = .None
        
//        let wrap = UIView(bgColor: UIColor.orangeColor())
        let wrap = UIView()
        
        contentView.addSubview(wrap)
        wrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(kConstraints.wrapEdges)
        }
        
        let line = UIView(bgColor: Define.separatorColor)
        contentView.addSubview(line)
        line.snp_makeConstraints { (make) -> Void in
            
            make.left.bottom.right.equalTo(contentView)
            make.height.equalTo(0.5)
        }
        
        
//        let topView = UIView(bgColor: UIColor.blueColor())
        let topView = UIView()
        
        wrap.addSubview(topView)
        topView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(wrap)
            make.height.equalTo(kConstraints.topViewHeight)
        }

//        let col1 = UIView(bgColor: xx_randomColor())
//        let col2 = UIView(bgColor: xx_randomColor())
//        let col3 = UIView(bgColor: xx_randomColor())
//        let col4 = UIView(bgColor: xx_randomColor())
        let col1 = UIView()
        let col2 = UIView()
        let col3 = UIView()
        let col4 = UIView()
        topView.addSubview(col1)
        topView.addSubview(col2)
        topView.addSubview(col3)
        topView.addSubview(col4)
        
        col1.snp_makeConstraints { (make) -> Void in
            
            make.top.left.bottom.equalTo(topView)
            make.width.equalTo(kConstraints.col1Width)
        }
        col2.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(topView)
            make.left.equalTo(col1.snp_right)
            make.width.equalTo(kConstraints.col2Width)
        }
        col3.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(topView)
            make.left.equalTo(col2.snp_right)
            make.width.equalTo(kConstraints.col3Width)
        }
        col4.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(topView)
            make.left.equalTo(col3.snp_right)
            make.width.equalTo(kConstraints.col4Width)
        }
        
        col1.addSubview(dayLabel)
        dayLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(col1).offset(xx_height(5))
            make.left.equalTo(col1)
        }
        col1.addSubview(hourLabel)
        hourLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(col1)
            make.top.equalTo(dayLabel.snp_bottom).offset(xx_height(-3))
        }
        
        col2.addSubview(moneyLabel)
        moneyLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(dayLabel)
            make.left.equalTo(col2)
        }
        
        col3.addSubview(balanceLabel)
        balanceLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(dayLabel)
            make.left.equalTo(col3)
        }

        
        col4.addSubview(typeLabel)
        typeLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(dayLabel)
            make.left.equalTo(col4)
        }
        
        col4.addSubview(indecatorBtn)
        indecatorBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(col4)
            make.centerY.equalTo(typeLabel)
        }
        
        wrap.addSubview(bottomView)
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(topView.snp_bottom)
            make.left.right.equalTo(wrap)
        }
        
        bottomView.addSubview(detailLabel)
        detailLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.left.equalTo(bottomView).offset(kConstraints.margin)
            make.right.equalTo(bottomView).offset(-kConstraints.margin)
            make.bottom.equalTo(bottomView).offset(xx_height(-15))
        }

//        let bottomView = UIView(bgColor: xx_randomColor())
//        
//        wrap.addSubview(detailLabel)
////        detailLabel.hidden = true
//        detailLabel.snp_makeConstraints { (make) -> Void in
//            
//            make.left.equalTo(wrap)
//            make.top.equalTo(topView.snp_bottom)
//            make.bottom.equalTo(wrap)
//        }
    }
    
    func rowHeight(viewModel: MyBillCellViewModel) -> CGFloat {
        
        self.baseCellViewModel = viewModel
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    lazy var bottomView = UIView(bgColor: UIColor.whiteColor())
    
    lazy var dayLabel = UILabel(num: "2015-11-12", fontSize: 12)
    lazy var hourLabel = UILabel(num: "16:33:40", fontSize: 12)
    lazy var moneyLabel = UILabel(num: "2000", fontSize: 12)
    lazy var balanceLabel = UILabel(num: "22,222,000.00", fontSize: 12)
    lazy var typeLabel = UILabel(title: "投资成功", fontSize: 12)
    lazy var detailLabel = UILabel(title: "备注:附近的收垃圾分类交罚款是垃圾分撒旦就家里发生空房间的萨拉飞机卡机发的是垃圾地方了", fontColor: Define.lightFontColor, bgColor: UIColor.whiteColor(), fontSize: 10, maxWrapWidth: kConstraints.detailLabelMaxWrapWidth)
    
    lazy var indecatorBtn: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "mybill_indector"), forState: .Normal)
        btn.rac_signalForControlEvents(.TouchUpInside).subscribeNext({ (sender) -> Void in
            
            xx_print(sender)
        })
        return btn
    }()
}
